<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
  <title>下拉刷新+无限加载</title>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
      background: #dadada;
    }
    
    header {
      height: 44px;
      line-height: 44px;
      text-align: center;
      background: #da020c;
      color: #fff;
    }
    
    header>a {
      height: 34px;
      line-height: 34px;
      position: absolute;
      top: 5px;
      left: 10px;
    }
    
    h1 {
      margin: 0;
      padding: 0;
      font-size: 18px;
      font-weight: normal;
    }
    
    #wrapper {
      position: absolute;
      top: 44px;
      bottom: 0;
      width: 100%;
    }
    
    #scroller {
      margin: 0;
      padding: 0;
    }
    
    .item {
      height: 60px;
      line-height: 60px;
      border-bottom: 1px solid #ddd;
      text-indent: 1em;
      background: #fff;
    }
    
    .jroll-infinite-tip {
      height: 44px;
      line-height: 44px;
      text-align: center;
    }
  </style>
  <script src="../../src/jroll.js"></script>
  <script src="../../plugins/jroll-pulldown/jroll-pulldown.js"></script>
  <script src="jroll-infinite.js"></script>
</head>

<body>
  <header>
    <h1>下拉刷新+无限加载</h1>
    <a onclick="javascript:history.back()">&lt;返回</a>
  </header>
  <div id="wrapper">
    <div id="scroller"></div>
  </div>

  <script>
    //模拟ajax
    function ajax(parms) {
      var a = [],
        i;
      var page = parms.url.replace("getData.do?page=", "");
      for (i = 1; i <= 20; i++) {
        a.push({
          "index": i + (page - 1) * 20,
          "text": "下拉刷新+无限加载"
        });
      }
      setTimeout(function() {
        parms.success(a);
      }, 800);
    }

    var jroll = new JRoll("#wrapper", {
      scrollBarY: true
    });

    //下拉刷新
    jroll.pulldown({
      refresh: function(complete) {
        jroll.options.page = 1;
	      jroll.scrollTo(0, 44, 0, true);  //滚回顶部
        ajax({
          url: "getData.do?page=" + 1,
          success: function(data) {
            complete();
            jroll.scroller.innerHTML = "";    //清空内容
            jroll.infinite_callback(data);
          }
        });
      }
    });

    //无限加载
    jroll.infinite({
      getData: function(page, callback) { //获取数据的函数，必须传递一个数组给callback
        ajax({
          url: "getData.do?page=" + page,
          success: function(data) {
            callback(data);
          }
        });
      },
      template: "<div class='item'>{{=_obj.index}}、{{=_obj.text}}</div>" //每条数据模板
    });
  </script>
</body>

</html>